home *** CD-ROM | disk | FTP | other *** search
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- (c) TechInsite Pty. Ltd.
- PO Box 429, Abbotsford, Melbourne. 3067 Australia
- Phone: +61 3 9419 6456
- Fax: +61 3 9419 1682
- Web: www.techinsite.com.au
- EMail: peter_hinrichsen@techinsite.com.au
-
- Created: Jan 2000
-
- Notes: Manage a family of visitors. Based on the factory pattern.
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
- unit tiPtnVisitorMgr ;
-
- interface
- uses
- Classes
- ,tiPtnVisitor
- ,tiDBConnection
- ,tiPtnVisitorDB
- ;
-
- type
-
- // TVisClassRef: A class reference type for storing visitor info
- //----------------------------------------------------------------------------
- TVisClassRef = class of TVisitorAbs ;
-
- // TVisMapping: There will be one of these for each visitor which has
- // been registered with the manager.
- //----------------------------------------------------------------------------
- TVisMapping = class( TObject )
- private
- FsGroupName : string ;
- FClassRef : TVisClassRef ;
- FVisitor : TVisitorAbs ;
- FDBConnection: TtiDBConnection;
- public
- constructor CreateExt( const psGroupName : string ;
- const pClassRef : TVisClassRef ) ;
- property GroupName : string read FsGroupName write FsGroupName ;
- property ClassRef : TVisClassRef read FClassRef write FClassRef ;
- property Visitor : TVisitorAbs read FVisitor write FVisitor ;
- property DBConnection : TtiDBConnection read FDBConnection write FDBConnection ;
- procedure Execute( pVisited : TVisitedAbs ) ;
- end ;
-
- // TVisitorCache: Manage the cached list of visitors.
- //----------------------------------------------------------------------------
- TVisitorCache = class( TObject )
- private
- FVisMappings : TStringList ;
- FDBConnection : TtiDBConnection ;
- public
- constructor create ;
- destructor destroy ; override ;
- // Register a new visitor with the manager.
- procedure RegisterVisitor( const psGroupName : string ;
- const pClassRef : TVisClassRef ) ;
- // Pass a TVisitedAbs, and execute a family of visitors as identified by
- // psGroupName
- procedure Execute( const psGroupName : string ;
- pVisited : TVisitedAbs ) ;
- end ;
-
- function gVisitorCache : TVisitorCache ;
-
- implementation
- uses
- SysUtils
- ,Windows
- ,tiUtils
- ;
-
- var
- uVisitorCache : TVisitorCache ;
-
- //------------------------------------------------------------------------------
- function gVisitorCache : TVisitorCache ;
- begin
- if uVisitorCache = nil then
- uVisitorCache := TVisitorCache.Create ;
- result := uVisitorCache ;
- end ;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisMgrCache
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- constructor TVisitorCache.create;
- begin
- inherited ;
- FVisMappings := TStringList.Create ;
- FDBConnection := TtiDBConnection.Create ;
- end;
-
- //------------------------------------------------------------------------------
- destructor TVisitorCache.destroy;
- var
- i : integer ;
- begin
- inherited;
-
- for i := 0 to FVisMappings.Count - 1 do
- TObject( FVisMappings.Objects[i] ).Free ;
-
- FVisMappings.Free ;
- FDBConnection.Free ;
-
- end ;
-
- //------------------------------------------------------------------------------
- procedure TVisitorCache.Execute(const psGroupName: string;pVisited : TVisitedAbs);
- var
- i : integer ;
- lsGroupName : string ;
- begin
- lsGroupName := upperCase( psGroupName ) ;
- for i := 0 to FVisMappings.Count - 1 do
- if FVisMappings.Strings[i] = lsGroupName then begin
- TVisMapping( FVisMappings.Objects[i] ).DBConnection := FDBConnection ;
- TVisMapping( FVisMappings.Objects[i] ).Execute( pVisited ) ;
- end ;
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisitorCache.RegisterVisitor( const psGroupName : string ;
- const pClassRef : TVisClassRef ) ;
- var
- lVisMapping : TVisMapping ;
- begin
- lVisMapping := TVisMapping.CreateExt( psGroupName, pClassRef ) ;
- FVisMappings.AddObject( lVisMapping.GroupName, lVisMapping ) ;
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisMapping
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- constructor TVisMapping.CreateExt(const psGroupName: string;
- const pClassRef: TVisClassRef);
- begin
- Create ;
- GroupName := upperCase( psGroupName ) ;
- ClassRef := pClassRef ;
- Visitor := nil ;
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisMapping.Execute( pVisited : TVisitedAbs ) ;
- begin
- Assert( pVisited <> nil, 'Visited not asigned' ) ;
- if Visitor = nil then
- Visitor := ClassRef.Create ;
- if Visitor is TVisDBAbs then
- TVisDBAbs( Visitor ).DBConnection := DBConnection ;
- pVisited.Iterate( Visitor ) ;
- if Visitor is TVisDBAbs then
- TVisDBAbs( Visitor ).DBConnection := nil ;
- end;
-
- initialization
-
- gVisitorCache ;
-
- finalization
- uVisitorCache.Free ;
-
- end.
-
-
-
-